package com.cloud.mall.sms.service.impl;

import com.cloud.mall.api.CommonPage;
import com.cloud.mall.base.BaseResponse;
import com.cloud.mall.core.bean.MallBeanUtils;
import com.cloud.mall.sms.input.SmsHomeRecommendSubjectInpDTO;
import com.cloud.mall.sms.mapper.SmsHomeRecommendSubjectMapper;
import com.cloud.mall.sms.model.SmsHomeRecommendSubject;
import com.cloud.mall.sms.model.SmsHomeRecommendSubjectExample;
import com.cloud.mall.sms.out.SmsHomeRecommendSubjectOut;
import com.cloud.mall.sms.service.SmsHomeRecommendSubjectService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SmsHomeRecommendSubjectServiceImpl implements SmsHomeRecommendSubjectService {
    @Autowired
    private SmsHomeRecommendSubjectMapper recommendProductMapper;

    @Transactional
    @Override
    public BaseResponse create(@RequestBody List<SmsHomeRecommendSubjectInpDTO> homeRecommendProductInpDTO) {
        List<SmsHomeRecommendSubject> recommendSubjectList=MallBeanUtils.copyDoToDto(homeRecommendProductInpDTO, SmsHomeRecommendSubject.class);
        for (SmsHomeRecommendSubject recommendProduct : recommendSubjectList) {
            recommendProduct.setRecommendStatus(1);
            recommendProduct.setSort(0);
            recommendProductMapper.insert(recommendProduct);
        }
        return BaseResponse.setResultSuccess(recommendSubjectList.size());
    }

    @Override
    public BaseResponse updateRecommendStatus(List<Long> ids,Integer recommendStatus) {
        SmsHomeRecommendSubjectExample example = new SmsHomeRecommendSubjectExample();
        example.createCriteria().andIdIn(ids);
        SmsHomeRecommendSubject record = new SmsHomeRecommendSubject();
        record.setRecommendStatus(recommendStatus);
        int count=recommendProductMapper.updateByExampleSelective(record,example);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }



    @Transactional
    @Override
    public BaseResponse delete(@RequestBody List<Long> ids) {
        SmsHomeRecommendSubjectExample example = new SmsHomeRecommendSubjectExample();
        example.createCriteria().andIdIn(ids);
        return BaseResponse.setResultSuccess(recommendProductMapper.deleteByExample(example));
    }

    @Override
    public BaseResponse updateSort(Long id, Integer sort) {
        SmsHomeRecommendSubject recommendProduct = new SmsHomeRecommendSubject();
        recommendProduct.setId(id);
        recommendProduct.setSort(sort);
        int count= recommendProductMapper.updateByPrimaryKeySelective(recommendProduct);
        if(count==0){
            return BaseResponse.setResultError();
        }
        return BaseResponse.setResultSuccess(count);
    }

    @Override
    public BaseResponse<CommonPage<SmsHomeRecommendSubjectOut>> list(String subjectName, Integer recommendStatus, Integer pageSize, Integer pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        SmsHomeRecommendSubjectExample example = new SmsHomeRecommendSubjectExample();
        SmsHomeRecommendSubjectExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(subjectName)){
            criteria.andSubjectNameLike("%"+subjectName+"%");
        }
        if(recommendStatus!=null){
            criteria.andRecommendStatusEqualTo(recommendStatus);
        }
        example.setOrderByClause("sort desc");
        List<SmsHomeRecommendSubject> homeBrandList= recommendProductMapper.selectByExample(example);
        return BaseResponse.setResultSuccess(CommonPage.restPage(MallBeanUtils.copyDoToDto(homeBrandList, SmsHomeRecommendSubjectOut.class)));
    }

}
